package com.test.annotation;

/*
 * Java注解
 * =============================================================
 * Version          Author          Date            Description
 * 1.0              白建         2018/1/31             创建
 * =============================================================
 */

import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface UserCase
{

    int id();

    String description() default "no description";
}

class PasswordUtils
{

    @UserCase(id = 47, description = "Password must contain at least one numeric")
    public boolean validatePassword(String password)
    {

        return (password.matches("\\w*\\d\\w*"));
    }

    @UserCase(id = 48)
    public String encryptPassword(String password)
    {

        return new StringBuilder(password).reverse().toString();
    }

    @UserCase(id = 49, description = "New passwords can't equal previously used ones")
    public boolean checkForNewPassword(List<String> prevPasswords, String password)
    {

        return !prevPasswords.contains(password);
    }
}

public class An_0
{

    public static void trackUseCases(List<Integer> useCases, Class<?> cl)
    {

        for (Method m : cl.getDeclaredMethods())
        {
            UserCase uc = m.getAnnotation(UserCase.class);
            if (uc != null)
            {
                System.out.println("Found Use Case: " + uc.id() + " " + uc.description());
                useCases.remove(new Integer(uc.id()));
            }
        }
        for (int i : useCases)
        {
            System.out.println("Warning: Missing use case-" + i);
        }
    }

    public static void main(String[] args)
    {

        List<Integer> userCases = new ArrayList<Integer>();
        Collections.addAll(userCases, 47, 48, 49, 50);
        trackUseCases(userCases, PasswordUtils.class);
    }
}
